import {
	hex_md5
} from '@/utils/md5'

const urlConfig = ''

let dev = false

const request = {}
let token = uni.getStorageSync('token')
let ua = uni.getSystemInfoSync().platform
let market = ''
let version_no = '1.0.0'
if (/ios/i.test(ua)) {
	market = 'ios'
} else if (/android/i.test(ua)) {
	
	market = 'android'
}

// #ifdef MP-WEIXIN
market = 'wx'
// #endif

// #ifdef APP-PLUS
plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
	version_no = wgtinfo.version
})
// #endif

const KEY = "UZTGOnclf.,KzV3f0xr:TsBn0s4Db3pLP#@"

let headers = {
	Authorization: token,
	market: market,
	version_no: version_no,
	"Content-Type": 'application/x-www-form-urlencoded'
}
// request请求封装
request.globalRequest = (url, data, method) => {
	const time = Date.now()
	headers.APIToken = hex_md5(hex_md5(time + KEY) + "hx4o~}sV<2")
	return uni
		.request({
			url: urlConfig + url,
			method: method,
			data: {
				...data,
				time,
				token: uni.getStorageSync('token'),
			},
			dataType: 'json',
			header: headers,
		})
		.then((res) => {
			if (res.header.Authorization) {
				uni.setStorageSync('token', res.header.Authorization)
			}
			if (res.data.code === 200) {
				return res.data
			} else if (res.data.code === 400) {
				uni.showToast({
					title: res.msg,
					duration: 2000,
					icon: 'none',
				})
				return res.data
			} else if (res.data.code === 403) {
				// 清理缓存
				uni.removeStorageSync('user_id')
				uni.removeStorageSync('avatar')
				uni.removeStorageSync('nickname')
				uni.removeStorageSync('mobile')
				uni.removeStorageSync('token')
				// 跳转登录页面
				uni.navigateTo({
					url: '/pages/user/login',
				})
			} else {
				if (!res.data.msg) return res.data
				// 显示请求失败信息
				uni.showToast({
					title: res.data.msg,
					duration: 2000,
					icon: 'none',
				})
				return res.data
			}
		})
		.catch((params) => {
			throw params
		})
}
// post请求
request.post = (url, data) => {
	return request.globalRequest(url, data, 'POST')
}
// get请求
request.get = (url, data) => {
	return request.globalRequest(url, data, 'GET')
}
// put请求
request.put = (url, data) => {
	return request.globalRequest(url, data, 'PUT')
}
// get请求
request.del = (url, data) => {
	return request.globalRequest(url, data, 'DELETE')
}

// 本地测试API http://192.168.1.7/box/public/index.php/api/
// 线上测试API http://test.qixdian.cn/api/
// 正式API http://www.qixdian.cn/api/
// 请求地址

if (process.env.NODE_ENV !== 'development') {
	// 生产环境
	if (dev) {
		// 如果出现未及时修改参数的情况，及时修改dev字段
		dev = false
	}
}

// h5页面配置，用于快速修改

// #ifdef H5
request.url = dev ? '/api/' : 'https://www.dingxians.cn/api/'
// #endif


// 打包到手机测试配置，用于查看实际显示效果
// #ifdef APP-PLUS
request.url = dev ? 'http://192.168.1.7/new_fashion_box/public/index.php/api/' : 'https://www.dingxians.cn/api/'
// #endif


// #ifdef MP-WEIXIN
request.url = dev ? 'http://192.168.1.7/new_fashion_box/public/index.php/api/' : 'https://www.dingxians.cn/api/'
// #endif

export default request